import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.util.StringUtils;

import java.io.*;

/**
 * 创建时间：2018-09-09
 * <p>修改时间：2018-09-09
 * <p>类说明：
 *
 * @author peng
 * @version 1.0
 */
public class SerInt {
    private static byte[] serialize(Writable w) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DataOutputStream dataOut = new DataOutputStream(out);
        w.write(dataOut);
        dataOut.close();
        return out.toByteArray();
    }

    private static byte[] deserialize(Writable w, byte[] bytes) throws IOException {
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        DataInputStream dataIn = new DataInputStream(in);
        w.readFields(dataIn);
        dataIn.close();
        return bytes;
    }

    public static void main(String[] args) throws Exception {
        IntWritable intW = new IntWritable(7);
        byte[] bytes = serialize(intW);
        String byteStr = StringUtils.byteToHexString(bytes);
        System.out.println(byteStr);

        IntWritable intW2 = new IntWritable(0);
        deserialize(intW2, bytes);
        System.out.println(intW2);
    }
}
